package gazzera.swing;

import gazzera.gui.messages.*;
import gazzera.swing.components.*;
import gazzera.swing.components.Box;
import gazzera.swing.model.*;
import gazzera.swing.utility.*;
import gazzera.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

public class AddShareFolderWindow extends JFrame implements ActionListener
{
	private final JLabel banner;
	private final JTextField input;
	private final Gazzera gazzera;
	private final Share share;
	private final HintTextField newgroup;
	private Map<String, JCheckBox> cbs;

	public AddShareFolderWindow(final Gazzera gazzera, Share share) throws HeadlessException
	{
		super();
		
		if (share == null)
		{
			setTitle("Add new shared folder");
			share = new Share(-1, "", new ArrayList<String>());
		}
		else
		{
			setTitle("Change shared folder");
		}
		this.share = share;
		this.gazzera = gazzera;
		setSize(400, 200);
		setIconImage(new ImageIcon(ResourceLoader.get("icon.png")).getImage());
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		Box b = new Box(true);
		//banner = new JLabel("<html>"+nickname+"<br><small style=\"color:#303030\">"+hash+"</small></html>");
		banner = new JLabel("Select a folder to share");
		banner.setMinimumSize(new Dimension(10, 20));
		b.add(banner, false);
				
		Box b2 = new Box(false);
		
		input = new JTextField();
		input.setText(share.folder);
		b2.add(input, true);
		
		JButton button = new JButton("Browse", new ImageIcon(ResourceLoader.get("folder.png")));
		button.setBorderPainted(false);
		button.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

				int returnVal = fc.showDialog(AddShareFolderWindow.this, "Select folder");
				if(returnVal == JFileChooser.APPROVE_OPTION )
				{
					input.setText(fc.getSelectedFile().getAbsolutePath());
				}
				
			}
		});
		b2.add(button, false);
		b.add(b2, false);
		
		
		JLabel banner2 = new JLabel("Limit visibility to selected groups (no selection means visible to everybody):");
		banner2.setMinimumSize(new Dimension(10, 20));
		b.add(banner2, false);
		
		JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
		
		
		cbs = new HashMap<String,JCheckBox>(); 
		for (String g : gazzera.allGroups)
		{
			JCheckBox j1 = new JCheckBox(g);
			j1.setSelected(share.groups.contains(g));
			cbs.put(g, j1);
			p.add(j1);
		}
		
		newgroup = new HintTextField("Enter new group");
		newgroup.setColumns(15);
		p.add(newgroup);
		
		b.add(p, true);
		
		JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				AddShareFolderWindow.this.dispose();
				
			}
		});
		p2.add(cancel);
		JButton ok = new JButton("Ok");
		ok.addActionListener(this);
		p2.add(ok);
		b.add(p2, false);
		add(b);
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		// clicked ok!
		String folder = input.getText();
		File f = new File(folder);
		if(!f.exists())
		{
			JOptionPane.showMessageDialog(this, "Selected folder doesn't exists");
			return;
		}
		if(!f.isDirectory())
		{
			JOptionPane.showMessageDialog(this, "Selected file is not a folder");
			return;
		}
		
		List<String> groups = new ArrayList<String>();
		for (String group : cbs.keySet())
		{
			JCheckBox x = cbs.get(group);
			if (x.isSelected())
			{
				groups.add(group);
			}
		}
		if(!Str.isEmpty(newgroup.getText()))
		{
			String ng = Str.strip(newgroup.getText().trim());
			groups.add(ng);
			gazzera.allGroups.add(ng);
		}
		gazzera.gui.pass(new EditShare(share.id, folder, groups));
		
		this.dispose();
		
	}

	
	
	

}
